package com.atguigu.gmall.product.controller;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.model.product.*;
import com.atguigu.gmall.product.service.ManageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/admin/product")
//@CrossOrigin    //解决跨域
public class BaseManageController {

    //调用服务层接口
    @Autowired
    private ManageService manageService;

    //获取一级分类数据
    @GetMapping("getCategory1")
    public Result getCategory1(){
        //调用服务层方法
        List<BaseCategory1> baseCategory1List = manageService.getCategory1();
        return Result.ok(baseCategory1List);
    }

    //获取二级分类数据
    @GetMapping("getCategory2/{category1Id}")
    public Result getCategory2(@PathVariable Long category1Id){
        //调用服务层方法
        List<BaseCategory2> baseCategory2List = manageService.getCategory2(category1Id);
        return Result.ok(baseCategory2List);
    }

    //获取三级分类数据
    @GetMapping("getCategory3/{category2Id}")
    public Result getCategory3(@PathVariable Long category2Id){
        //调用服务层方法
        List<BaseCategory3> baseCategory3List = manageService.getCategory3(category2Id);
        return Result.ok(baseCategory3List);
    }

    //根据分类id获取平台属性集合
    @GetMapping("attrInfoList/{category1Id}/{category2Id}/{category3Id}")
    public Result getAttrInfoList(@PathVariable Long category1Id,
                                  @PathVariable Long category2Id,
                                  @PathVariable Long category3Id){
        //调用服务层方法
        //平台属性中，应该包含平台属性值集合
        List<BaseAttrInfo> baseAttrInfoList = manageService.getAttrInfoList(category1Id,category2Id,category3Id);
        //返回数据
        return Result.ok(baseAttrInfoList);
    }

    //保存平台属性
    @PostMapping("saveAttrInfo")
    public Result saveAttrInfo(@RequestBody BaseAttrInfo baseAttrInfo){
        //调用服务层方法
        manageService.saveAttrInfo(baseAttrInfo);
        //返回
        return Result.ok();
    }

    //根据平台属性id获取平台属性值集合
    @GetMapping("getAttrValueList/{attrId}")
    public Result getAttrValueList(@PathVariable Long attrId){
        //调用服务层方法
        //获取数据：本质直接获取这个表中的数据
        //select * from base_attr_value where attr_id = arrtId;
        //可以更加复合业务：可以从平台属性中获取平台属性值集合
        //select * from base_arrt_into where id = arrtId;
        BaseAttrInfo baseAttrInfo = manageService.getBaseAttrInfo(attrId);
        //List<BaseAttrValue> baseAttrValueList = manageService.getAttrValueList(attrId);
        return Result.ok(baseAttrInfo.getAttrValueList());
    }
}
